package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;

/**
 * Created by yhj on 17/1/18.
 */
public class MultiMapperRenamePlugin extends PluginAdapter {
    private Pattern searchExamplePattern;
    private String replaceExampleString;


    private Pattern searchMapperPattern;
    private String replaceMapperString;


    private String exampleSubPackage;
    private String modelSubPackage;


    private String modelSuffix;

    /**
     *
     */
    public MultiMapperRenamePlugin() {
    }

    public boolean validate(List<String> warnings) {


        String searchExampleString = properties.getProperty("searchExampleString");

        String searchMapperString = properties.getProperty("searchMapperString");

        exampleSubPackage = properties.getProperty("exampleSubPackage");

        modelSubPackage = properties.getProperty("modelSubPackage");

        modelSuffix = properties.getProperty("modelSuffix");

        validateExampleReplace(searchExampleString.split(":"));

        validateMapperReplace(searchMapperString.split(":"));


        return true;
    }

    private void validateExampleReplace(String[] searchString) {
        if (searchString.length == 2) {
            boolean valid = stringHasValue(searchString[0])
                    && stringHasValue(searchString[1]);

            if (valid) {
                searchExamplePattern = Pattern.compile(searchString[0]);
                replaceExampleString = searchString[1];
            }
        }
    }

    private void validateMapperReplace(String[] searchString) {
        if (searchString.length == 2) {
            boolean valid = stringHasValue(searchString[0])
                    && stringHasValue(searchString[1]);

            if (valid) {
                searchMapperPattern = Pattern.compile(searchString[0]);
                replaceMapperString = searchString[1];
            }
        }
    }

    @Override
    public void initialized(IntrospectedTable introspectedTable) {

        {   // example的名字修改一下
            String oldType = introspectedTable.getExampleType();
            Matcher matcher = searchExamplePattern.matcher(oldType);
            oldType = matcher.replaceAll(replaceExampleString);

            addSubPackage(oldType,exampleSubPackage);

            introspectedTable.setMyBatis3JavaMapperType(oldType);
        }



        {   //mapper 的名字修改
            String oldType = introspectedTable.getMyBatis3JavaMapperType();
            Matcher matcher = searchMapperPattern.matcher(oldType);
            oldType = matcher.replaceAll(replaceMapperString);

            introspectedTable.setMyBatis3JavaMapperType(oldType);
        }



        {   // 检查model 的后缀
            String oldType = introspectedTable.getTableType();

            if(modelSuffix != null && modelSuffix.trim().length() > 0 ){
                if (!oldType.endsWith(modelSuffix)) {
                    oldType = oldType+modelSuffix;
                }
            }

            addSubPackage(oldType,modelSubPackage);
            introspectedTable.setTableType(oldType);
        }
    }


    private String addSubPackage(String name ,String sub){
        if(sub != null && sub.trim().length() > 0){

            int aa = name.lastIndexOf(".");

            name = name.substring(0,aa) + "." + sub + "."+name.substring(aa);
        }

        return name;
    }
}